package com.mh.config;

import com.mh.common.UserLoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * Date:2024/9/2
 * author:zmh
 * description: 注册自定义拦截器
 **/

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    /**
     * 注册自定义拦截器
     * @param registry ·
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserLoginInterceptor())
                .excludePathPatterns("/userInfos/login/**") // 放行登录请求
                .excludePathPatterns("/userInfos/register/**") // 放行注册请求
                .excludePathPatterns("/common/getEmailVerificationCode/**") // 放行注册获取邮箱验证码接口
//                .excludePathPatterns("/userInfos/resetPassword/**") // 放行修改密码接口
                .addPathPatterns("/**"); // 拦截的地址;// 拦截的地址
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                .allowedOriginPatterns("*")
                //是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                //跨域允许时间
                .maxAge(3600);
    }

}
